package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.*;
import cn.wolfcode.crm.mapper.StoragesMapper;
import cn.wolfcode.crm.query.StoragesQueryObject;
import cn.wolfcode.crm.service.IStoragesService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class StoragesServiceImpl implements IStoragesService {

    @Autowired
    private StoragesMapper storagesMapper;
    public static HttpSession getSession() {
        // 获取session方法
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        // 强转
        ServletRequestAttributes sra = (ServletRequestAttributes) requestAttributes;
        HttpSession session = sra.getRequest().getSession();
        return session;
    }
    public void deleteByPrimaryKey(Long id) {
        storagesMapper.deleteByPrimaryKey(id);
    }

    public void insert(Storages record) {
        storagesMapper.insert(record);
    }

    public Storages selectByPrimaryKey(Long id) {
        return storagesMapper.selectByPrimaryKey(id);
    }

    public List<Storages> selectAll() {

        return storagesMapper.selectAll();
    }

    public void updateByPrimaryKey(Storages record) {
        storagesMapper.updateByPrimaryKey(record);
    }

    public PageResult query(StoragesQueryObject qo) {
        int total = storagesMapper.queryFormCount(qo);
        if(total == 0){
            return new PageResult();
        }
        List<Storages> list = storagesMapper.queryFormList(qo);
        return new PageResult(total,list);
    }

    public void saveStorages(Long storages_id,Long agency_id, Long product_id, Double limit, Double durations, Client client, CarInfo info) {
        System.out.println(storages_id+":"+agency_id+":"+product_id+":"+limit+":"+durations+":"+client+":"+info);
        Employee currentUser = (Employee) SecurityUtils.getSubject().getPrincipal();
        if(client != null) {
            Storages storages = new Storages();
            AgencyInfo agencyInfo = new AgencyInfo();
            ProductInfo productInfo = new ProductInfo();
            Client clients = new Client();
            agencyInfo.setId(agency_id);
            productInfo.setId(product_id);
            storages.setAgencyInfos(agencyInfo);
            storages.setProductInfos(productInfo);
            storages.setLimits(limit);
            storages.setDurations(durations);
            storages.setUsername(info.getUsername());
            storages.setCars(info.getCar());
            storages.setCarnum(info.getCarnum());
            storages.setPurchasedate(info.getPurchasedate());
            clients.setId(client.getId());
            storages.setClients(clients);
            storages.setEmployees(currentUser);
            storages.setNumbers(UUID.randomUUID().toString().replaceAll("-", "").substring(1, 10));
            storages.setPut(new Date());
            storages.setState(Storages.STATE_DEFAULT);
            storagesMapper.insert(storages);
            getSession().removeAttribute("client");
        }
        else{
            Storages storages = new Storages();
            AgencyInfo agencyInfo = new AgencyInfo();
            ProductInfo productInfo = new ProductInfo();
            agencyInfo.setId(agency_id);
            productInfo.setId(product_id);
            storages.setAgencyInfos(agencyInfo);
            storages.setProductInfos(productInfo);
            storages.setLimits(limit);
            storages.setDurations(durations);
            storages.setUsername(info.getUsername());
            storages.setCars(info.getCar());
            storages.setCarnum(info.getCarnum());
            storages.setPurchasedate(info.getPurchasedate());
            storages.setId(storages_id);
            storagesMapper.updateByPrimaryKey(storages);



        }
    }

    public void storagesIdsStoraId(Long id, Integer state, Integer pay) {
        storagesMapper.storagesIdsStoraId(id,state,pay);
    }

    public void storagesIdsStora(Storages storages) {
        storagesMapper.storagesIdsStora(storages.getPaymentTime(),storages.getId(),storages.getMethod());
    }
    /*
    * 填写事故备案表之后,将accident_state改成1
    * */
    public void changeAccidenState(String insuranceNo) {
        storagesMapper.changeAccidenState(insuranceNo);
    }


}
